//
//  LoginVC.m
//  TestAll
//
//  Created by wen on 2020/8/7.
//  Copyright © 2020 ZDHS. All rights reserved.
//

#import "LoginVC.h"
#import "Masonry.h"
#import "RC.h"
#import "AppDelegate.h"
#import <HYBNetworking.h>

@interface LoginVC ()

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    
    UITextField *tf = UITextField.new;
    tf.placeholder = @"请输入用户id:1-10登录";
    tf.text = @"6";
    tf.layer.borderWidth = 1;
    tf.layer.borderColor = UIColor.lightGrayColor.CGColor;
    tf.tag = 101;
    [self.view addSubview:tf];
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setTitle:@"登  录" forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:btn];
    
    [tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.offset(Height_NavBar+100);
        make.width.equalTo(@200);
        make.height.equalTo(@40);
    }];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(tf.mas_bottom).offset(40);
        make.width.height.equalTo(tf);
    }];
    
}
-(void)btnAction:(UIButton *)btn{
    
    UITextField *tf = [self.view viewWithTag:101];
    if(tf.text.length==0){
        kshowMsg(@"请输入用户id");
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *url = @"http://hongqi.wengegroup.com:9115/api/user/getUserRegister";
    NSString *username = [NSString stringWithFormat:@"用户%@",tf.text];
    NSDictionary *dic = @{@"userId":tf.text,@"userName":username};
    
    [HYBNetworking configRequestType:(kHYBRequestTypeJSON) responseType:(kHYBResponseTypeJSON) shouldAutoEncodeUrl:NO callbackOnCancelRequest:NO];
    [HYBNetworking postWithUrl:url refreshCache:NO params:dic success:^(id response) {
        NSArray *arr = response[@"data"];
        NSDictionary *dic = arr.firstObject;
        [[RCIM sharedRCIM] connectWithToken:dic[@"token"] dbOpened:^(RCDBErrorCode code) {
            NSLog(@"");
        }success:^(NSString *userId) {
            kshowMsg(@"登录成功");
            RCUserInfo *userInfo = [[RCUserInfo alloc] initWithUserId:userId name:username portrait:@"http://www.wengegroup.com/img/logo/LOGO_new.png"];
            [RCIM sharedRCIM].currentUserInfo = userInfo;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [CurAppDelegate mainRootVC];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            });
        }error:^(RCConnectErrorCode status) {
            NSLog(@"登录失败");
            kshowMsg(@"登录失败");
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            });
        }];
    } fail:^(NSError *error) {
        NSLog(@"");
    }];
    
    
    
    
}

@end
